package a_aa_amainpackage.a_o2odata_deal.utils

import org.apache.spark.sql.{DataFrame, SQLContext}
import a_aa_amainpackage.a_o2odata_deal.config.config._
import com.alibaba.fastjson.JSON
import org.apache.spark.sql.functions.lit
import a_aa_amainpackage.a_o2odata_deal.config.FieldsConfig._
/*
*项目名: o2odata_commend
*文件名: conandvar_deal
*创建者: hancongcong
*创建时间:2019/8/20 11:39
*描述: TODO
*/ object conandvar_deal {

  /**
    * 变量和常量关联
    * @param sqlContext
    * @param sourceselldata
    * @return
    */
  def tmCVcommite(sqlContext:SQLContext,sourceselldata:DataFrame): DataFrame ={

    //val sourceVardata = sqlContext.read.json(s"${source_obs_data_path}")
    val sourceVardata = sqlContext.read.json(s"${source_obs_data_path}")
      .drop("timeStamp")
      .withColumn("platformName", lit("天猫"))
      .withColumn("platformId", lit("10"))
      .withColumn("timeStamp", lit(s"${timeStamp}"))
      .withColumn("shopType",lit("B"))
    val sourceVardatas = sourceVardata.toJSON.rdd.map(line => {
      val nObject = JSON.parseObject(line)
      val preSaleCount = nObject.getOrDefault("preSaleCount", "-1")
      val detailCategoryName = nObject.getOrDefault("detailCategoryName", "-1")
      val earnest = nObject.getOrDefault("earnest", "-1")
      val is_o2o = nObject.getOrDefault("is_o2o", "-1")
      nObject.put("preSaleCount", preSaleCount)
      nObject.put("detailCategoryName", detailCategoryName)
      nObject.put("earnest", earnest)
      nObject.put("is_o2o", is_o2o)
      nObject.toJSONString
    })
    val sourceVardatass = sqlContext.read.json(sourceVardatas)
    //原始数据
    sourceselldata.registerTempTable("sourceselldata")
    sourceVardatass.registerTempTable("sourceVardata")

    //企业
    // 原始数据提取
    val priceResdfVCs = sqlContext.sql(
      s"""
        |select
        |${varfields}
        |t2.sellCount,
        |t2.priceText,
        |cast(t2.salesAmount as decimal(20,2)) salesAmounts
        |from
        |sourceVardata t1
        |join
        |sourceselldata t2
        |on t1.good_id = t2.good_id
      """.stripMargin).drop("salesAmount")
      .withColumnRenamed("salesAmounts","salesAmount")
      .dropDuplicates("good_id")
    return priceResdfVCs
  }

}
